package com.zhuangfei.hpuhelper.view;

import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hpu.bean.Book;
import com.hpu.bean.Borrow;
import com.hpu.constants.HttpConstants;
import com.hpu.constants.ShareConstants;
import com.hpu.parser.BookBorrowParser;
import com.hpu.parser.BookParser;
import com.hpu.service.BookBorrowService;
import com.hpu.service.BookService;
import com.hpu.service.LoginService;
import com.hpu.tools.ShareTools;
import com.zhuangfei.generalpage.BaseActivity;
import com.zhuangfei.hpuhelper.tools.DefaultHandler;
import com.zhuangfei.toolkit.IntentToolkit;
import com.zhuangfei.toolkit.tools.ActivityTools;
import com.zhuangfei.toolkit.tools.BundleTools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.lzf.serviceplatform.R;

public class BookBorrowActivity extends BaseActivity {

    private BookBorrowService borrowService;
    private LoginService loginService;

    String id;

    @BindView(R.id.id_borrow_name)
    TextView nameTextView;

    @BindView(R.id.id_borrow_index)
    TextView indexTextView;

    @BindView(R.id.id_borrow_listview)
    public ListView listView;
    List<Map<String, String>> items = new ArrayList<>();
    SimpleAdapter simpleAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_borrow);
        ActivityTools.setTransparent(this);
        ButterKnife.bind(this);
        inits();
    }

    private void inits() {
        loginService=new LoginService(this,handler);
        borrowService=new BookBorrowService(handler, this);

        id= BundleTools.getString(this,"id","0");
        String name=BundleTools.getString(this,"name","书名未获取到");
        String index=BundleTools.getString(this,"index","索书号未获取到");

        nameTextView.setText(name);
        indexTextView.setText("索书号:"+index);


        simpleAdapter=new SimpleAdapter(this,items,R.layout.item_borrow_layout,
                new String[]{"info","status"},
                new int[]{R.id.item_borrow_info,R.id.item_borrow_status});
        listView.setAdapter(simpleAdapter);

        borrowService.getBorrow(id);
    }

    DefaultHandler handler = new DefaultHandler(this) {
        public void handleMessage(Message msg) {
            getViewHelper().showSuccessView();
            super.handleMessage(msg);

            switch (msg.what) {
                case HttpConstants.QUERY_BOOK_BORROW_SUCCESS:
                    Log.i("Borrow",msg.obj.toString());
                    BookBorrowParser parser=new BookBorrowParser();
                    List<Borrow> result=parser.parse(msg.obj.toString());
                    showResult(result);
                    break;

                // VPN过期
                case HttpConstants.VPN_TOOLONG:
                    if(handler.isIgnoreVpnExpired()){
                        break;
                    }
                    getViewHelper().showLoadingView();
                    loginService.login(ShareTools.get(BookBorrowActivity.this, ShareConstants.STORE_USER, "").toString(),
                            ShareTools.get(BookBorrowActivity.this, ShareConstants.STORE_PASSWORD1, "").toString(),
                            ShareTools.get(BookBorrowActivity.this, ShareConstants.STORE_PASSWORD2, "").toString());
                    break;

                // 登录成功
                case HttpConstants.LOGIN_SUCCESS:
                    getViewHelper().showLoadingView();
                    ShareTools.put(BookBorrowActivity.this, ShareConstants.STORE_LOGINCOOKIE, (String) msg.obj);
                    borrowService.getBorrow(id);
                    break;

            }
        };
    };

    private void showResult(List<Borrow> result) {
        if(result==null||result.size()==0){
            getViewHelper().showEmptyView("该书不可借");
        }

        items.clear();

        for(Borrow borrow:result){
            Map<String,String> map=new HashMap<>();
            String stack=borrow.getStack();
            String room=borrow.getRoom();
            String status=borrow.getStatus();
            if(stack==null){
                stack="";
            }
            if(room==null){
                room="";
            }
            if(status==null){
                status="";
            }
            String info=stack+room;
            map.put("info",info);
            map.put("status",status);
            items.add(map);
        }

        simpleAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoading() {
        getViewHelper().showLoadingView();
        borrowService.getBorrow(id);
    }

    @OnClick(R.id.id_back)
    public void goBack(){
        new IntentToolkit().with(this)
                .target(SearchBookActivity.class)
                .canBack()
                .execute();
    }

    @Override
    public void onBackPressed() {
        goBack();
    }
}
